﻿using System.Windows;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.UnityExtensions;
using StockholmsStad.Windows.UI.Controllers;
using StockholmsStad.Windows.UI.Infrastructure.Services;
using StockholmsStad.Windows.UI.Interfaces;
using StockholmsStad.Windows.UI.Modules.Ceremony;
using StockholmsStad.Windows.UI.Modules.Couple;
using StockholmsStad.Windows.UI.Modules.MainModule;
using StockholmsStad.Windows.UI.Modules.Maintenance;
using StockholmsStad.Windows.UI.Modules.Minister;
using StockholmsStad.Windows.UI.Modules.WatchList;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Infrastructure.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Windows.UI.Models;

namespace StockholmsStad.Windows.UI
{
    internal class Bootstrapper : UnityBootstrapper
    {
        private IShell view;
        private readonly User CurrentUser;

        public Bootstrapper(User user)
        {
            CurrentUser = user;
        }

        protected override void ConfigureContainer()
        {
            Container.RegisterType<IViewController, ViewController>();
            Container.RegisterType<IShellPresentationModel, ShellPresentationModel>();
            Container.RegisterType<IShell, Shell>();
            Container.RegisterType<IApplicationService, ApplicationService>();
            
            base.ConfigureContainer();
        }

        protected override DependencyObject CreateShell()
        {
            var shellPresenter = Container.Resolve<ShellPresenter>();
            view = shellPresenter.View;
            return view as DependencyObject;
        }

        protected override void InitializeModules()
        {
            var mainModule = Container.Resolve<MainModule>();
            mainModule.Initialize();

            var coupleModule = Container.Resolve<CoupleModule>();
            coupleModule.Initialize();
            // Default startup view
            coupleModule.LoadView();

            var ceremonyModule = Container.Resolve<CeremonyModule>();
            ceremonyModule.Initialize();

            var ministerModule = Container.Resolve<MinisterModule>();
            ministerModule.Initialize();

            if (CurrentUser.Role == UserRole.SysAdmin || CurrentUser.Role == UserRole.SuperUser)
            {
                var maintenanceModule = Container.Resolve<MaintenanceModule>();
                maintenanceModule.Initialize();
            }

            var watchListModule = Container.Resolve<WatchListModule>();
            watchListModule.Initialize();
        }

        public void ShowView()
        {
            view.ShowView();
        }
    }
}
